<?php
/*
fileHashDB
Copyright (c) 2010 The Hursk Group, LLC. All rights reserved.

www.hursk.com

hurskgroup@hursk.com

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

*/

function dbf_connectDB($config_data) {
	$db_link = mysqli_connect($config_data[db][host], $config_data[db][user], $config_data[db][password], $config_data[db][db_name], $config_data[db][port], $config_data[db][socket]);
	if(!$db_link) {
		$xml = c_initiate_xml($config_data);
		$xml_error = $xml->addchild('error');
		$xml_error->addchild('code', '500');
		$xml_error->addchild('description', 'DB Error trying to connect.');
		c_announce($xml);
	}
	//mysqli_autocommit($db_link, FALSE); // turning off auto commit to be safer.

	return $db_link;
}

function dbf_getFileHashData($db_link, $hashType, $hash, $config_data) {
	//echo $hashType." = ".$hash;
	$query = "SELECT * FROM fileHash WHERE $hashType = '$hash'";
	if(!$result = mysqli_query($db_link, $query)) {
		$xml = c_initiate_xml($config_data);
		$xml_error = $xml->addchild('error');
		$xml_error->addchild('code', '500');
		$xml_error->addchild('description', 'DB Error: '.mysqli_error($db_link));
		c_announce($xml);
	} else if(mysqli_num_rows($result) == 0) {
		$xml = c_initiate_xml($config_data);
		$xml_error = $xml->addchild('error');
		$xml_error->addchild('code', '400');
		$xml_error->addchild('description', 'Hash value is NOT in the database.');
		c_announce($xml);
	}
	
	// so it turns out that this hash is in the db and there wasn't an
	// error returned.  Lets get the result set.
	$fileHashData = mysqli_fetch_assoc($result);
	
	$fileHashData[queryHashType] = $hashType;
	$fileHashData[queryHash] = $hash;
	
	return $fileHashData;
}

function dbf_getNSRLHashData($db_link, $fileHashData, $config_data) {
	//
	// this function recieves hashType for the search string, but for
	// now only md5 in the NSRL dataset is indexed so, no matter
	// what is handed we will resort to md5.
	//
	//
	$query = "SELECT * FROM NSRLFile, new_NSRLMfg, new_NSRLOS, new_NSRLProd WHERE NSRLFile.MD5 = '$fileHashData[md5]' AND NSRLFile.ProductCode = new_NSRLProd.ProductCode AND new_NSRLProd.OpSystemCode = new_NSRLOS.OpSystemCode AND new_NSRLProd.MfgCode = new_NSRLMfg.MfgCode";
	if(!$result = mysqli_query($db_link, $query)) {
		$xml = c_initiate_xml($config_data);
		$xml_error = $xml->addchild('error');
		$xml_error->addchild('code', '500');
		$xml_error->addchild('description', 'DB Error: '.mysqli_error($db_link));
		c_announce($xml);
	} else if(mysqli_num_rows($result) == 0) {
		$xml = c_initiate_xml($config_data);
		$xml_error = $xml->addchild('error');	
		$xml_error->addchild('code', '400');
		$xml_error->addchild('description', 'Hash value is NOT in the database, even though the fileHash table said it was.');
		c_announce($xml);
	}
	
	
	// initiate the xml data structure
	$xml = c_initiate_xml($config_data);
	$nsrl_result = $xml->addchild('nsrl_result');
	$nsrl_result->addAttribute('query_hash_type', $fileHashData[queryHashType]);
	$nsrl_result->addAttribute('query_hash', $fileHashData[queryHash]);
	$file_info = $nsrl_result->addchild('file_details');

	$first_run = 1;
	while ($row = mysqli_fetch_assoc($result)) {
		// used to populate the hash values which shouldn't be different
		// (unless of crc32 collision) and will run first time
		// but not any other time through this loop
		if($first_run) {
			$hash_sha1 = $file_info->addchild('hash', $row[SHA1]);
			$hash_sha1->addAttribute('type', 'sha1');
			
			$hash_md5 = $file_info->addchild('hash', $row[MD5]);
			$hash_md5->addAttribute('type', 'md5');
			
			$hash_crc32 = $file_info->addchild('hash', $row[CRC32]);
			$hash_crc32->addAttribute('type', 'crc32');
			
			$file_size = $file_info->addchild('file_size', $row[FileSize]);
			
			$first_run = 0;
		}
		
		// get the fileName which is the higest level in the remaining
		// data set and check to see if it is in the xml path with xpath
		// below will be a list of known filenames
		// filename -> ProductCode -> OpSystemCode
		$active_file_name_node = 0;
		foreach ($file_info->children() as $fileName) {
			if($fileName == $row[FileName]) {
				$active_file_name_node = $fileName;
			}
		}
		// if fileName wasn't already found in the space so we need to add a new one
		if(!$active_file_name_node) {
			$active_file_name_node = $file_info->addchild('file_name', $row[FileName]);
		}
		
		//
		// check the product code
		//
		$active_product_name_node = 0;
		foreach ($active_file_name_node->children() as $productName) {
			if($productName == $row[ProductName]) {
				$active_product_name_node = $productName;
			}
		}
		// if product name wasn't found we create a new node
		if(!$active_product_name_node) {
			$active_product_name_node = $active_file_name_node->addchild('product_name', $row[ProductName]);
			$active_product_name_node->addAttribute('version', $row[ProductVersion]);
			// put product information into xml structure
			$active_product_name_node->addAttribute('language', $row[Language]);
			$active_product_name_node->addAttribute('application_type', $row[ApplicationType]);
		}
		
		
		//
		// opsystem name
		//
		$active_opsystem_node = 0;
		foreach ($active_product_name_node->children() as $opSystemName) {
			//echo $opSystemName;
			if($opSystemName == $row[OpSystemName]) {
				$active_opsystem_node = $opSystemName;
			}
		}
		// if opSystemCode name wasn't found we create a new node
		if(!$active_opsystem_node) {
			$active_opsystem_node = $active_product_name_node->addchild('operating_system', $row[OpSystemName]);
			$active_opsystem_node->addAttribute('version', $row[OpSystemVersion]);
		}
	}
	
	return $xml;
}

?>